package com.xiao.xin.controller;
import com.xiao.xin.service.ChatHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import java.util.function.Consumer;

/***
 * AI
 */
@Slf4j
@RestController
@RequestMapping("/ai")
public class ChatController {

    @Autowired
    private ChatClient client;

    @Autowired
    private ChatHistoryService chatHistoryService;

    @PostMapping(path = "/chat", produces = "text/html;charset=utf-8")
    public Flux<String> chat(String prompt, String chatId) {
        log.info("chatId:{}", chatId);
        chatHistoryService.addChatHistory("chat", chatId);
        return client.prompt().user(prompt).advisors(advisorSpec ->
                advisorSpec.param(ChatMemory.CONVERSATION_ID, chatId)).stream().content();}

}
